#include <FastSerial.h>
#define DEBUGPORT Serial
#define BUFFERSIZE 32
FastSerialPort0(DEBUGPORT);



//interrupts
#define ICP4PIN 49

//ICP4
volatile byte flagICP4;
ISR(TIMER4_CAPT_vect)
{
  if (!digitalRead(ICP4PIN)) {// magnet
    TCCR4B|=B01000000;//set rising edge
    flagICP4=1;
  }
  else { //no magnet
    TCCR4B&=B10111111;//set falling edge
    flagICP4=0;
  }
}

#define LEDPIN 13
#define VCCPIN 53
#define GNDPIN 51

void setup()
{
  pinMode(LEDPIN, OUTPUT);  //LED debug
  pinMode(VCCPIN, OUTPUT);  //VCCPIN
  pinMode(GNDPIN, OUTPUT); //GNDPIN
  digitalWrite(GNDPIN,LOW);
  digitalWrite(VCCPIN,HIGH);
  
  DEBUGPORT.begin(57600);
  
  //timer4 setup
  TCCR4A=0;
  TCCR4B=B00000001;//prescaler=1
  TCCR4C=0;
  
  //ICP4 ISR for hall effect
  pinMode(ICP4PIN, INPUT); //hall effect
  digitalWrite(ICP4PIN,HIGH); //internal pullup
  TCCR4B|=B11000000;//ICP filter plus rising
  TIMSK4|=B00100000;//ICP enable  




} 


  
void loop()
{
  
  //Misc variables
  byte i;
  int light=LOW;
  unsigned long t=millis();
 
  

  while(1){
    
    if (digitalRead(ICP4PIN)) {//no magnet
      //flagICP4=0;
    }
    else { //magnet
      //flagICP4=1;
    }

    
    if( (millis()-t) > 100) { // Main loop runs at 10Hz
      t=millis();
      light=!light;
      digitalWrite(13, light);   // set the LED on
      
      DEBUGPORT.println(flagICP4);

      
      
      
    }//end 10Hz
    
  }//end while(1)
}//end loop()
